import React from 'react'
import Table from '../../../components/Table'
import { Button } from 'antd'
import { connect } from 'react-redux'
import { ASK_FRIEND, FIND_FRIENDS } from '../constant'
import PubSub from 'pubsub-js'
import { setAskFriend, setBookRecommend } from '../../action/action'

class BorrowingRecord extends React.PureComponent {
  columns = [
    {
      title: '流水号',
      dataIndex: 'sernum',
      key: 'sernum',
      width: 90
    },
    {
      title: '书名',
      dataIndex: 'name',
      key: 'name',
      search: true
    },
    {
      title: '图书id',
      dataIndex: 'bookId',
      key: 'bookId',
      width: '80px',
      search: true
    },
    {
      title: '读者学号',
      dataIndex: 'readerId',
      key: 'readerId',
      search: true
    },
    {
      title: '借出日期',
      dataIndex: 'lendDate',
      key: 'lendDate',
      search: true
    },
    {
      title: '归还日期',
      dataIndex: 'backDate',
      key: 'backDate',
    },
    {
      title: '',
      width: '100px',
      align: 'center',
      key: 'findFriends',
      render: (text, record) => {
        return (<Button onClick={() => this.findFriends('findFriends', record)}>找书友</Button>)
      }
    },
    {
      title: '',
      width: '100px',
      align: 'center',
      key: 'askStu',
      render: (text, record) => {
        return (<Button onClick={() => this.findFriends('askStu', record)}>问同学</Button>)
      }
    },

  ]

  constructor (props) {
    super(props)

  }

  findFriends = (tag, data) => {
    const { bookId, readerId } = data
    const { dispatch } = this.props
    if (tag === 'findFriends') {
      dispatch(setBookRecommend(bookId, readerId))
      // 找书友
      PubSub.publish(FIND_FRIENDS)
    } else if (tag === 'askStu') {
      dispatch(setAskFriend(bookId, readerId))
      // 问同学
      PubSub.publish(ASK_FRIEND)
    }

  }

  render () {
    const { height, tableData } = this.props
    return (
      <Table
        rowKey={'sernum'}
        columns={this.columns}
        height={height}
        className={'table-height'}
        scroll={{ y: height ? height - 90 : '' }}
        dataSource={tableData.toJS()}
        pagination={{
          showTotal: (total, range) => `第${range[0]}-${range[1]}条/共${total}条`,
          defaultPageSize: 10,
          showSizeChanger: true,
          pageSizeOptions: [10, 20, 50, 100, 200, 500],
        }}
      />
    )
  }
}

const mapStateToProps = (state) => {
  return {
    tableData: state.get('getBorrowingRecordTableData'),
    height: state.get('getHeight')
  }
}
export default connect(mapStateToProps)(BorrowingRecord)